package com.hm.system.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.annotation.AuthenticationAnnotation;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.common.CommonUtil;
import com.hm.common.model.ControllerResult;
import com.hm.common.model.PageInfo;
import com.hm.common.util.CommonDef.OperationStatus;
import com.hm.system.mongo.domain.def.SysUserDef;
import com.hm.system.mongo.vo.system.SysUserInfoVo;
import com.hm.system.share.service.elasticsearch.system.EsSysUserInfoService;
import com.hm.system.share.service.mongo.system.SysUserInfoService;
import com.hm.system.util.SystemConfigDef.Api.Rest;
import com.hm.system.web.su.BaseController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 2016年5月12日 下午9:59:25
 * @version 1.0
 * @describe 系统用户管理
 */
@ResponseBody
@RestController
@RequestMapping(value = Rest.Sys.REST_SYS_USER_INFO)
public class SysUserInfoController extends BaseController {

	public static final String swaggerTag = "系统用户管理";

	@Autowired
	private SysUserInfoService sysUserInfoService;
	
	@Autowired
	private EsSysUserInfoService esSysUserInfoService;

	@AuthenticationAnnotation
	@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
	@ApiOperation(value = "获取系统用户", httpMethod = "GET", notes = "获取系统用户通过ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取系统用户", content = "获取系统用户通过ID")
	public ResponseEntity<ControllerResult<SysUserInfoVo>> getSysUserInfo(
			@ApiParam(required = true, name = "userId", value = "系统用户ID") @PathVariable String userId) {
		try {
			return ControllerResult.success(sysUserInfoService.getSysUserInfo(userId),OperationStatus.LOAD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
	
	@RequestMapping(value="/register", method = RequestMethod.POST)
	@ApiOperation(value = "用户注册", httpMethod = "POST", notes = "系统用户注册,前端用户注册", tags = swaggerTag)
	@RequestPlanAnnotation(title = "用户注册", content = "系统用户注册,前端用户注册")
	public ResponseEntity<ControllerResult<String>> registerSysUserInfo(
			@ApiParam(required = true, name = "body", value = "系统用户信息") @RequestBody(required = true) SysUserInfoVo sysUserInfo) {
		try {
			if(sysUserInfo.getUserDef().equals(SysUserDef.ADMINISTRATOR)){
				return ControllerResult.failed("用户注册接口不支持系统管理员注册");
			}
			String id = sysUserInfoService.registerSysUserInfo(sysUserInfo, currentUserId());
			return ControllerResult.success(id,OperationStatus.ADD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "添加系统用户", httpMethod = "POST", notes = "添加系统用户返回添加成功的用户ID,[该用户用于登录后台系统]", tags = swaggerTag)
	@RequestPlanAnnotation(title = "添加系统用户", content = "添加系统用户返回添加成功的用户ID,[该用户用于登录后台系统]")
	public ResponseEntity<ControllerResult<String>> addSysUserInfo(
			@ApiParam(required = true, name = "body", value = "系统用户信息") @RequestBody(required = true) SysUserInfoVo sysUserInfo) {
		try {
//			if (ValidateUtil.validate(sysUserInfo)) {
				String id = sysUserInfoService.addSysUserInfo(sysUserInfo, currentUserId());
				if (StringUtils.isEmpty(id)) {
					return ControllerResult.failed(OperationStatus.ADD_FAIL);
				}
				
				{//TODO 测试代码
					esSysUserInfoService.saveEsSysUserInfo(id);
					esSysUserInfoService.queryTest();
				}
				return ControllerResult.success(id, OperationStatus.ADD_SUCCESSFUL);
//			}
//			return ControllerResult.failed("添加失败");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
	
	@AuthenticationAnnotation
	@RequestMapping(value = "/{userId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改系统用户", httpMethod = "PUT", notes = "修改系统用户返回修改成功的用户ID,[该用户用于登录后台系统]", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改系统用户", content = "修改系统用户返回修改成功的用户ID,[该用户用于登录后台系统]")
	public ResponseEntity<ControllerResult<String>> updateSysUserInfo(
			@ApiParam(required = true, name = "body", value = "系统用户信息") @RequestBody(required = true) SysUserInfoVo sysUserInfo,
			@ApiParam(required = true, name = "userId", value = "系统用户ID") @PathVariable String userId) {
		try {
//			if (ValidateUtil.validate(sysUserInfo)) {
				sysUserInfo.setId(userId);
				String userInfoId = sysUserInfoService.updateSysUserInfo(sysUserInfo, currentUserId());
				return ControllerResult.success(userInfoId, OperationStatus.UPDATE_SUCCESSFUL);
//			}
//			return ControllerResult.failed("修改失败");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除系统用户", httpMethod = "DELETE", notes = "删除系统用户", tags = swaggerTag)
	@RequestPlanAnnotation(title = "删除系统用户", content = "修改系统用户")
	public ResponseEntity<ControllerResult<String>> deleteSysUserInfo(
			@ApiParam(required = true, name = "userId", value = "系统用户ID") @PathVariable String userId) {
		try {
			SysUserInfoVo sysUserInfo = sysUserInfoService.getSysUserInfo(userId);
			if (null == sysUserInfo) {
				return ControllerResult.failed("目标删除数据不存在,删除失败");
			}

			String id = sysUserInfoService.deleteSysUserInfo(sysUserInfo, currentUserId());
			return ControllerResult.success(id, OperationStatus.DELELETE_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(value = "获取系统用户数据", httpMethod = "GET", notes = "分页获取系统用户数据", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取系统用户数据", content = "分页获取系统用户数据")
	public ResponseEntity<ControllerResult<PageInfo<SysUserInfoVo>>> querySysUserInfo(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = true, name = "userDef", value = "用户类型") @RequestParam("userDef") SysUserDef userDef,
			@ApiParam(required = false, name = "name", value = "姓名") @RequestParam(required = false) String name,
			@ApiParam(required = false, name = "mobile", value = "电话号码") @RequestParam(required = false) String mobile,
			@ApiParam(required = false, name = "startTime", value = "开始时间") @RequestParam(required = false) Long startTime,
			@ApiParam(required = false, name = "endTime", value = "结束时间") @RequestParam(required = false) Long endTime) {
		try {
			return ControllerResult.success(
					sysUserInfoService.querySysUserInfo(userDef,name, mobile, startTime, endTime, pageNumber, pageSize));
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
	
	@AuthenticationAnnotation
	@RequestMapping(value = "/pwd/{userId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改用户登录密码", httpMethod = "PUT", notes = "修改系统用户登录密码", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改用户登录密码", content = "修改系统用户登录密码")
	public ResponseEntity<ControllerResult<String>> updatePassword(
			@ApiParam(required = true, name = "userId", value = "用户id") @PathVariable("userId") String userId,
			@ApiParam(required = true, name = "oldPwd", value = "旧密码") @RequestParam(required = true) String oldPwd,
			@ApiParam(required = true, name = "newPwd", value = "新密码") @RequestParam(required = true) String newPwd){
		try {
			if(CommonUtil.isAnyEmpty(userId,oldPwd,newPwd)){
				return ControllerResult.failed("请求路径为/pwd/{userId},并且旧密码和新密码不能为空");
			}
			return ControllerResult.success(sysUserInfoService.updatePassword(userId, oldPwd, newPwd),"密码修改成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
	
	@AuthenticationAnnotation
	@RequestMapping(value = "/hpt/{userId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改用户头像", httpMethod = "PUT", notes = "修改系统用户头像", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改用户头像", content = "修改系统用户头像")
	public ResponseEntity<ControllerResult<String>> updateHeadPortrait(
			@ApiParam(required = true, name = "userId", value = "用户id") @PathVariable("userId") String userId,
			@ApiParam(required = true, name = "url", value = "头像访问地址") @RequestParam(required = true) String url){
		try {
			if(CommonUtil.isAnyEmpty(userId,url)){
				return ControllerResult.failed("请求路径为/pwd/{userId},并且用户头像不能为空");
			}
			return ControllerResult.success(sysUserInfoService.updateHeadPortrait(userId, url),"修改头像成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
}
